package com.zhutong.main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Line;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.SlideMaster;
import org.apache.poi.hslf.model.Table;
import org.apache.poi.hslf.model.TableCell;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;

public class PoiPPT {
	public static void main(String[] args) throws Exception {
		// setColor();
		//readPPT();
		read2PPT();
		//read3PPT();
	}

	private static void setColor() throws Exception {
		SlideShow ppt = new SlideShow();
		Slide newSlide = ppt.createSlide();
		TextBox title = newSlide.addTitle();// ��ӻõ�Ƭ����
		int picIndex = 0;
		SlideMaster master = null;
		RichTextRun titleRun = title.getTextRun().getRichTextRuns()[0];
		titleRun.setFontColor(Color.RED);
		title.setText("ppt����");
		// /////////////////////////////////////////////
		try {
			ppt.setPageSize(new Dimension(760, 600));// ���ûõ�Ƭ��С
			master = ppt.getSlidesMasters()[0];
			picIndex = ppt.addPicture(new File("img/background.png"),
					Picture.PNG);// ����ĸ�屳��,֧�ֶ���ͼƬ��ʽ
		} catch (IOException e) {
			e.printStackTrace();
		}
		Picture background = new Picture(picIndex);// ����ͼƬλ��
		background.setAnchor(new java.awt.Rectangle(0, 0,
				ppt.getPageSize().width, ppt.getPageSize().height));
		master.addShape(background);
		// //////////////////////////////////////////
		TextBox txt = new TextBox();// ����ı���
		RichTextRun richTextRun = txt.getTextRun().getRichTextRuns()[0];
		richTextRun.setFontColor(Color.BLUE);
		richTextRun.setText("������Ի���\r\n�ڶ����ı�");// setText�����ַ������԰����س������з�,�������һ�в�����\\r\\n��β,�������õĸ�ʽû��Ч��(v3.5)
		txt.setAnchor(new java.awt.Rectangle(50, 150, 400, 400));
		newSlide.addShape(txt);
		// /////////////////////////////////////////////
		picIndex = ppt.addPicture(new File("img/img.jpg"), Picture.JPEG);
		Picture jpg = new Picture(picIndex);
		jpg.setAnchor(new java.awt.Rectangle(360, 200, 280, 260)); // set image
																	// position
																	// in the
																	// slide
		newSlide.addShape(jpg);
		// //////////////////////////////////////////
		String[][] datas = { { "���", "����", "����" }, { "1", "����", "30" },
				{ "2", "����", "27" } };
		Table table = new Table(3, 3); // create a table of 3 rows and 3
										// columnsTable
		for (int i = 0; i < datas.length; i++) {
			for (int j = 0; j < datas[i].length; j++) {
				TableCell cell = table.getCell(i, j);
				RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
				rt.setFontName("����");
				rt.setFontSize(12);
				cell.setVerticalAlignment(TextBox.AnchorMiddle);
				cell.setHorizontalAlignment(TextBox.AlignCenter);
				cell.setText(datas[i][j]);
				if (i == 0) {
					cell.setFillColor(Color.GRAY);// ���б�������Ϊ��ɫ
				}
			}
		}
		Line border = table.createBorder();
		border.setLineColor(Color.black);
		border.setLineWidth(2.0);
		table.setAllBorders(border);
		newSlide.addShape(table);
		table.moveTo(160, 260);
		// ////////////////////////////////////////////

		savePPTFile(ppt);
	}

	private static void savePPTFile(SlideShow ppt) throws Exception {
		FileOutputStream out = new FileOutputStream("d:/test/testppt.ppt");
		ppt.write(out);
		out.close();
	}

	private static void readPPT() throws Exception {
		SlideShow ppt = new SlideShow(new HSLFSlideShow("e:/docs/number.ppt"));
		Dimension pgsize = ppt.getPageSize();
		
		Slide[] slide = ppt.getSlides();
		
		System.out.println(slide.length);
		
		for (int i = 0; i < slide.length; i++) {
			BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D graphics = img.createGraphics();
			graphics.setPaint(Color.white);// clear the drawing area
			graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,
					pgsize.height));
			slide[i].draw(graphics);
			FileOutputStream out = new FileOutputStream("png/slide-" + (i + 1)
					+ ".png");
			javax.imageio.ImageIO.write(img, "png", out);
			out.close();
		}
	}

	private static void read2PPT() throws Exception {
		SlideShow ppt = new SlideShow(new HSLFSlideShow("e:/docs/number.ppt"));
		Slide[] slides = ppt.getSlides();// ��ȡ�ı���Ϣ
		
		System.out.println("---------------- [" + slides.length + "] -----------------");
		
		/*for (int k = 0 ; k < slides.length; k++) {
			Slide each = slides[k];
			//System.out.println(each.getTitle());
			TextRun[] textRuns = each.getTextRuns();
			
//			System.out.println("---------------- [" + textRuns.length + "] -----------------");
//			System.out.println(textRuns[textRuns.length-1].getText());
			for (int i = 0; i < textRuns.length; i++) {
				System.out.println(textRuns[i].getText());
				RichTextRun[] richTextRuns = textRuns[i].getRichTextRuns();
				for (int j = 0; j < richTextRuns.length; j++) {
					System.out.println(richTextRuns[j].getText());
				}
			}
		}*/
		
		PictureData[] picDatas = ppt.getPictureData();
		for (int i = 0; i < picDatas.length; i++) {
				if(i != 0 && i != 1 && i != 2) {
					
					String name = "jpg/";
					
					Slide each = slides[i-3];
					TextRun[] textRuns = each.getTextRuns();
					
					//System.out.println("---------------- [" + textRuns.length + "] -----------------");
					
					if(textRuns.length == 3) {
						name += textRuns[1].getText() + ".jpg";
					}
					else if(textRuns.length == 4) {
						name += textRuns[1].getText() + "" + textRuns[2].getText() + ".jpg";
					}
				
					System.out.println(name);
					
					FileOutputStream out = new FileOutputStream(name);
					out.write(picDatas[i].getData());
					out.close();
				}
				
		}
		
		/*PictureData[] picDatas = ppt.getPictureData();
		System.out.println(picDatas.length);
		for (int i = 0; i < 10; i++) {
			System.out.println(picDatas[i].getType());
			FileOutputStream out = new FileOutputStream("jpg/jpg_" + i
					+ ".jpg");
			//ppt.write(out);
			out.write(picDatas[i].getData());
			out.close();
			if (picDatas[i].getType() == Picture.PNG) {
				FileOutputStream out = new FileOutputStream("jpg/jpg_" + i
						+ ".jpg");
				ppt.write(out);
				out.close();
			}
		}*/
		
		/*for (Slide each : slides) {
			System.out.println(each.getTitle());
			TextRun[] textRuns = each.getTextRuns();
			for (int i = 0; i < textRuns.length; i++) {
				System.out.println(textRuns[i].getText());
				RichTextRun[] richTextRuns = textRuns[i].getRichTextRuns();
				for (int j = 0; j < richTextRuns.length; j++) {
					System.out.println(richTextRuns[j].getText());
				}
			}
		}*/
		// ��ȡ����JPEGͼƬ
		/*PictureData[] picDatas = ppt.getPictureData();
		for (int i = 0; i < picDatas.length; i++) {
			if (picDatas[i].getType() == Picture.PNG) {
				FileOutputStream out = new FileOutputStream("jpg/jpg_" + i
						+ ".jpg");
				ppt.write(out);
				out.close();
			}
		}*/
	}

	private static void read3PPT() throws Exception {
		HSLFSlideShow hslf = HSLFSlideShow.create();
		DocumentSummaryInformation dsi = hslf.getDocumentSummaryInformation();  
		SummaryInformation si = hslf.getSummaryInformation();    
		dsi.setCompany("yourCompany");  
		dsi.setCategory("ppt����");  
		si.setAuthor("yourName");  
		si.setTitle("����");   
		SlideShow ppt = new SlideShow(hslf);
		savePPTFile(ppt);
	}
}
